package com.shop.shop_end.utils.redis;

import com.shop.shop_end.entity.MessageJson;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

public class RedisUtils {

    @Resource
    private static RedisCache redisCache;

    public static Set<String> getKeysByPattern(String pattern) {
        String newPattern = "*" + pattern + "*";
        Set<String> keys = redisCache.getKeysByPattern(newPattern);
        return keys;
    }

    public static List<MessageJson> getAllKeyAndValue(String pattern){
        Collection<String> keys = RedisUtils.getKeysByPattern(pattern);
        List<MessageJson> messageJsonList = new ArrayList<>();
        for (String key : keys) {
            List<MessageJson> value = redisCache.getCacheList(key);
            System.out.println("Key: " + key + ", Value: " + value);
            messageJsonList.addAll(value);
            redisCache.deleteObject(key);//在redis里删除此聊天记录
        }
        return messageJsonList;
    }

}
